package algorithm.linear;

/**
 * 链式队列
 * @param <E>
 */
public class LinkedQueue<E> implements QQueue<E> {
    private Node<E> front,rear;

    public LinkedQueue() {
        this.front = this.rear = null;
    }

    public boolean isEmpty() {
        return this.front == null && this.rear == null;
    }

    public boolean enqueue(E element) {
        if ( element == null )
            return false;
        Node<E> q = new Node(element);
        if (!isEmpty()){
            this.rear.next = q;
        } else {
            this.front = q;
        }
        this.rear = q;
        return true;
    }

    public E dequeue() {
        if (!isEmpty()){
            E temp = this.front.data;
            this.front = this.front.next;
            if (this.front ==null)
                this.rear = null;
            return temp;
        }
        return null;
    }

    @Override
    public String toString() {
        return "LinkedQueue{" +
                "front=" + front +
                ", rear=" + rear +
                '}';
    }
}
